package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.User;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条

        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询
        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageResult getByToUserId(Long toUserId, Integer page, Integer pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(toUserId));
        long count = mongoTemplate.count(query, RecommendUser.class);
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setItems(recommendUsers);
        pageResult.setPagesize(pagesize);
        pageResult.setCounts(count);
//        pageResult.setPages();
        return pageResult;
    }

    @Override
    public RecommendUser getByToUserId(Long userId, Long toUser) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId).and("toUserId").is(toUser));
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId,int count) {
        //查询所有喜欢或不喜欢的用户
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);
        //构建查询，排除不喜欢的用户
            //构造条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserId);
        //统计查询筛选
        TypedAggregation<RecommendUser> typedAggregation = TypedAggregation.newAggregation(
                RecommendUser.class,
                Aggregation.match(criteria),
                Aggregation.sample(count));
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(typedAggregation, RecommendUser.class);
        List<RecommendUser> mappedResults = results.getMappedResults();
        return mappedResults;
    }
}
